import numpy as np
from gymnasium import utils
from mujoco_py.mujoco_wrapper import MujocoEnv
from gymnasium.spaces import Box

DEFAULT_CAMERA_CONFIG = {
    "trackbodyid": 0,
    "distance": 5.04,
}


def normalize_radian(rad: float) -> float:
    mod_rad = rad % (2 * np.pi)  # 约束到 [0, 2π)
    if mod_rad > np.pi:  # 如果超过 π，减去 2π
        mod_rad -= 2 * np.pi
    return mod_rad


class MujocoPendulum(MujocoEnv, utils.EzPickle):
    metadata = {
        "render_modes": [
            "human",
            "rgb_array",
            "depth_array",
        ],
        "render_fps": 25,
    }

    def __init__(self, **kwargs):
        utils.EzPickle.__init__(self, **kwargs)
        observation_space = Box(low=-np.inf, high=np.inf, shape=(3,), dtype=np.float32)
        MujocoEnv.__init__(
            self,
            model_path="../xmls/my_Pendulum.xml",
            frame_skip=2,
            observation_space=observation_space,
            default_camera_config=DEFAULT_CAMERA_CONFIG,
            **kwargs,
        )

    def step(self, a):
        a = np.array(a).reshape(self.action_space.shape)
        # 仿真
        self.do_simulation(a, self.frame_skip)
        next_state = self._get_obs()
        reward = self._calc_reward(a)
        if self.render_mode == "human":
            self.render()
        info = {
            "theta(deg)": float(normalize_radian(self.data.qpos))
        }
        return next_state, reward, False, False, info

    def reset_model(self):
        qpos = self.init_qpos + self.np_random.uniform(
            size=self.model.nq, low=-0.01, high=0.01
        )
        qvel = self.init_qvel + self.np_random.uniform(
            size=self.model.nv, low=-0.01, high=0.01
        )
        self.set_state(qpos, qvel)
        return self._get_obs()

    def _calc_reward(self, action):
        theta = normalize_radian(self.data.qpos.copy())
        theta_dot = self.data.qvel.copy()
        return -theta ** 2 - 0.1 * theta_dot ** 2 - 0.001 * action ** 2

    def _get_obs(self):
        return np.concatenate([np.cos(self.data.qpos), np.sin(self.data.qpos), self.data.qvel],dtype=np.float32).ravel()


if __name__ == "__main__":
    env = MujocoPendulum(render_mode="human")
    env.reset()
    for _ in range(200):
        next_state, reward, dw, tr, info = env.step(.01)
        print(reward)
    env.close()
